﻿using System.Text;

namespace MyTRCP.Common.Models
{
    public class ChannelInfo : BitWalkableObj
    {
        public ModulationTypeEnum TxModulation { get; set; }
        public int TxFreq { get; set; }
        public ModulationTypeEnum RxModulation { get; set; }
        public int RxFreq { get; set; }
        public short TxSubAudio { get; set; }
        public short RxSubAudio { get; set; }
        public bool Scan { get; set; }
        public bool TxPowerPart1 { get; set; }
        public bool Offline { get; set; }
        public bool Bandwidth { get; set; }
        public bool PreDeEmphBypass { get; set; }
        public bool Sign { get; set; }
        public bool TxPowerPart2 { get; set; }
        public bool TxDisable { get; set; }
        public bool FixedFreq { get; set; }
        public bool FixedBandwidth { get; set; }
        public bool FixedTxPower { get; set; }
        public bool Mute { get; set; }
        public string Name { get; set; } = string.Empty;
        public byte TxColor { get; set; }
        public byte RxColor { get; set; }
        public byte Slot { get; set; }
        public int TxPower
        {
            get
            {
                if (TxPowerPart1)
                {
                    return 0;
                }
                return TxPowerPart2 ? -2 : -1;
            }
            set
            {
                switch (value)
                {
                    case 0:
                        TxPowerPart1 = true;
                        TxPowerPart2 = false;
                        break;
                    case -1:
                        TxPowerPart1 = false;
                        TxPowerPart2 = false;
                        break;
                    case -2:
                        TxPowerPart1 = false;
                        TxPowerPart2 = true;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException(nameof(value), "TxPower数值不在范围内");
                }
            }
        }
        public ChannelInfo(byte[] data):base(data)
        {
        }
        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            int dmrLen = (RxModulation == ModulationTypeEnum.DMR || TxModulation == ModulationTypeEnum.DMR) ? 2 : 0;
            bw.WriteBits((byte)TxModulation, 2)
                .WriteBits(TxFreq, 30)
                .WriteBits((byte)RxModulation, 2)
                .WriteBits(RxFreq, 30)
                .WriteBits(TxSubAudio, 16)
                .WriteBits(RxSubAudio, 16)
                .WriteBit(Scan)
                .WriteBit(TxPowerPart1)
                .WriteBit(Offline)
                .WriteBit(Bandwidth)
                .WriteBit(PreDeEmphBypass)
                .WriteBit(Sign)
                .WriteBit(TxPowerPart2)
                .WriteBit(TxDisable)
                .WriteBit(FixedFreq)
                .WriteBit(FixedBandwidth)
                .WriteBit(FixedTxPower)
                .WriteBit(Mute)
                .SkipBits(4)
                .WriteBytes(Encoding.GetEncoding(936).GetBytes(Name));
            if (dmrLen > 0)
            {
                bw.SetPos(192)
                .WriteBits(TxColor, 4)
                .WriteBits(RxColor, 4)
                .WriteBits(Slot, 1);
            }
            return bw;
        }
        public override string ToString()
        {
            return $"txModulation:{TxModulation};txFreq:{TxFreq};rxModulation:{RxModulation};rxFreq:{RxFreq};txSubAudio:{TxSubAudio};rxSubAudio:{RxSubAudio};scan:{Scan};txPower:{TxPower};offline:{Offline};bandwidth:{Bandwidth};pre_de_emph_bypass:{PreDeEmphBypass};sign:{Sign};txDisable:{TxDisable};fixed_freq:{FixedFreq};fixed_bandwidth:{FixedBandwidth};fixed_tx_power:{FixedTxPower};mute:{Mute};name:{Name};tx_color:{TxColor};rx_color:{RxColor};slot:{Slot}";
        }

        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            if (bw.ByteLen < 24)
                throw new ArgumentException("字节数组长度过小（24）", nameof(bw));
            TxModulation = (ModulationTypeEnum)bw.ReadBits(2);
            TxFreq = (int)bw.ReadBits(30);
            RxModulation = (ModulationTypeEnum)bw.ReadBits(2);
            RxFreq = (int)bw.ReadBits(30);
            TxSubAudio = (short)bw.ReadBits(16);
            RxSubAudio = (short)bw.ReadBits(16);
            Scan = bw.ReadBit();
            TxPowerPart1 = bw.ReadBit();
            Offline = bw.ReadBit();
            Bandwidth = bw.ReadBit();
            PreDeEmphBypass = bw.ReadBit();
            Sign = bw.ReadBit();
            TxPowerPart2 = bw.ReadBit();
            TxDisable = bw.ReadBit();
            FixedFreq = bw.ReadBit();
            FixedBandwidth = bw.ReadBit();
            FixedTxPower = bw.ReadBit();
            Mute = bw.ReadBit();
            bw.SkipBits(4);
            Name = Encoding.GetEncoding(936).GetString(bw.ReadBytes(10)).Replace("\0", "");
            if (bw.ByteLen >= 26)
            {
                bw.SetPos(192);
                TxColor = (byte)bw.ReadBits(4);
                RxColor = (byte)bw.ReadBits(4);
                Slot = (byte)bw.ReadBits(1);
            }
            return bw;
        }
    }
}